package view;

import Dao.CSVDao;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class CSVGUI extends JFrame {

    private JPanel contentPane;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    CSVGUI frame = new CSVGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public CSVGUI() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JLabel lblNewLabel = new JLabel("\u6587\u4EF6\u5BFC\u51FA\u8DEF\u5F84");

        textField = new JTextField();
        textField.setColumns(10);

        JButton btnNewButton = new JButton("\u5BFC\u51FA\u62A5\u8868");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                CSVactionPerformed(e);
            }
        });
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGap(84)
                                .addComponent(lblNewLabel)
                                .addGap(18)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                        .addComponent(btnNewButton)
                                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, 203, GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(47, Short.MAX_VALUE))
        );
        gl_contentPane.setVerticalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGap(62)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel)
                                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(ComponentPlacement.RELATED, 82, Short.MAX_VALUE)
                                .addComponent(btnNewButton)
                                .addGap(63))
        );
        contentPane.setLayout(gl_contentPane);
    }

    private void CSVactionPerformed(ActionEvent e) {
        String path = textField.getText();
        System.out.println(path);
        if (path.isEmpty()){
            JOptionPane.showMessageDialog(null,"请输入导出路径");
            return;
        }
        boolean b = false;
        String[] paths = {path+"\\MEMBERCARD.csv",path+"\\TAKINGCARD.csv",path+"\\MEMBER.csv",};
        String[] sql = {"select * from MEMBERCARD","select * from TAKINGCARD","select * from MEMBER"};
        for (int i = 0; i< sql.length;i++){
            b = CSVDao.createCSV(paths[i],sql[i]);
        }
        if (b == true){
            JOptionPane.showMessageDialog(null,"导出成功");
        } else {
            JOptionPane.showMessageDialog(null,"导出失败");
        }
    }

}
